// 2013-10-24上午10:21:51  net.betou.trade.admin.action.DataMoveAct.java

package net.betou.trade.admin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.common.util.TradeUtils;
import net.betou.common.web.RequestUtils;
import net.betou.common.web.ResponseUtils;
import net.betou.core.entity.ConfigLogin;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.main.manager.MoveDataMng;
import net.betou.trade.main.manager.agencyorproxy.AdminMng;
import net.betou.trade.web.ResponseUtil;
import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * 
 * @author 叶装装
 */
@Controller
public class DataMoveAct{
	
	@RequestMapping( "/data/v_list.jspx" )
	public String toMove( HttpServletRequest request, ModelMap model ){
		model.addAttribute( "exts", ConfigLogin.moveDataMap );
		return "data/list";
	}
	
	@RequestMapping( "/data/o_move.jspx" )
	public void move( String [ ] moveDatas, String fromUser, String toUser, HttpServletRequest request, HttpServletResponse response, ModelMap model ){
		ResponseUtil util = validateMove( moveDatas, fromUser, toUser, response );
		
		if( util.hasResponse( ) ){
			util.renderJson( );
			return;
		}
		
		AgentAndAdmins fromAdmin = adminMng.findByUsername( fromUser );
		AgentAndAdmins toAdmin = adminMng.findByUsername( toUser );
		
		String msg = "";
		
		// 要转移的数据
		for( String move : moveDatas ){
			// user  BaseTrade  notice  BaseContent  category
			int update = moveMng.move( move, fromAdmin, toAdmin );
			msg += "移动【" + move + "】数据" + ( update > 0 ? "成功" : "失败") ;
		}
		
		
		util.addSuccessResponse( msg );
		util.renderJson( );
		return;
	}
	
	private ResponseUtil validateMove( String [] moveDatas, String fromUser, String toUser, HttpServletResponse response ){
		ResponseUtil util = ResponseUtil.create( response );
		
		if( StringUtils.isBlank( fromUser ) || StringUtils.isBlank( toUser ) ){
			util.addErrorResponse( "转移用户不存在" );
			return util;
		}
		
		if( StringUtils.equals( fromUser, toUser ) ){
			util.addErrorResponse( "From To 不可一样" );
			return util;
		}
		
		if( moveDatas == null || moveDatas.length == 0 ){
			util.addErrorResponse( "请选择要移动的数据" );
			return util;
		}
		
		AgentAndAdmins fromAdmin = adminMng.findByUsername( fromUser );
		
		if( fromAdmin == null ){
			util.addErrorResponse( "From 用户不存在" );
			return util;
		}
		
		AgentAndAdmins toAdmin = adminMng.findByUsername( toUser );
		
		if( toAdmin == null ){
			util.addErrorResponse( "To 用户不存在" );
			return util;
		}
		
		return util;
	}
	
	@RequestMapping( "/data/v_check_username.jspx" )
	public void checkUsername( HttpServletRequest request, HttpServletResponse response ){
		
		String username = request.getParameter( "fromUser" );
		
		if( StringUtils.isBlank( username )){
			username = request.getParameter( "toUser" );
		}
		
		username = RequestUtils.parseParamUsingUTF8( username );

		if( StringUtils.isBlank( username ) ){
			ResponseUtils.renderJson( response, Boolean.FALSE.toString( ) );
			return;
		}
		ResponseUtils.renderJson( response, String.valueOf( !adminMng.userNameNotExist( username )) );
		return;
	}
	
	@RequestMapping( "/data/v_user.jspx" )
	public void  listuser( String term, HttpServletRequest request, HttpServletResponse response ){
		if( StringUtils.isNotBlank( term ) ){
			term = RequestUtils.parseParamUsingUTF8( term );
			ResponseUtils.renderJson( response, JSONArray.fromObject( adminMng.findListByUserName( TradeUtils.getAdmin( request ), term ) ).toString( ) );
		}
	}
	
	@Autowired
	private AdminMng adminMng;
	
	@Autowired
	private MoveDataMng moveMng;
}
